package com.offcn.service;

import com.offcn.dao.CourseDao;
import com.offcn.dao.CourseDaoImpl;
import com.offcn.entity.Course;
import com.offcn.utlis.PageUtils;

import java.util.List;

public class CourseByServiceImpl implements CourseByService {
   // 调用dao层
    CourseDao  courseDao = new CourseDaoImpl();

    /**
     * 添加课程
     * @param course
     * @return
     */
    @Override
    public boolean add(Course course) {
        return  courseDao.add(course)>0;
    }

    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @param search
     * @return
     */
    @Override
    public PageUtils<Course> find(int currentPage, int pageSize, String search) {
     int total  = courseDao.findAll("select * from course where courseName like ? ","%"+search+"%" ).size();

        List<Course> courseList = courseDao.findAll("select * from course where courseName like ? limit  ?,?", "%" + search + "%", (currentPage - 1) * pageSize, pageSize);
        PageUtils<Course> pageUtils = new PageUtils<Course>(pageSize,currentPage,total,courseList);
        return pageUtils;
    }

    /**
     * 修改
     * @param course
     * @return
     */
    @Override
    public boolean updateCourse(Course course) {
        return courseDao.updateCourse(course)>0;
    }

    /**
     * 删除
     * @param cids
     * @return
     */
    @Override
    public boolean delete(String[] cids) {
        try {
            for (String cid : cids) {
             int k  = courseDao.del(cid);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
        return true;
    }

    @Override
    public List<Course> findAll() {
        return courseDao.findAllCourse();
    }
}
